// clisp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "global.h"

void test()
{
    initOperator();
    try
    {
        cout << eval("(defun a (y) (+ y 2))") << endl;
        cout << eval("(a 3)") << endl;
    }
    catch (const string& e)
    {
        cerr << e << endl;
    }
    catch (const char* e)
    {
        cerr << e << endl;
    }


}

void console()
{
    bool loop = true;
    
    initOperator();
    initExtension();

    string str;    
    size_t cmdno = 1;

    while (loop)
    {
        try
        {
            cin.sync();
            cout << "[" << cmdno << "#]:";
            getline(cin, str);
            if (str.size() == 0)
            {
                continue;
            }
            cout << cmdno++ << "> " << eval(str) << endl << endl;
        }
        catch (const string& e)
        {
            cerr << "err:" << e << endl;
        }
        catch (const char* e)
        {
            cerr << "err:" << e << endl;
        }
        catch (const Command& cmd)
        {
            if ((string)cmd == "exit")
            {
                loop = false;
            }
            else if ((string)cmd == "clear")
            {
#ifdef WINDOWS
                system("cls");
#else
                system("clear");
#endif
            }
            else if ((string)cmd == "ls")
            {
#ifdef WINDOWS
                system("dir");
#else
                system("ls");
#endif
            }
            else if ((string)cmd == "pwd")
            {
#ifdef WINDOWS
                system("cd");
#else
                system("pwd");
#endif
            }
            else
            {
                cout << (string)cmd << endl;
            }
        }
        catch (...)
        {
            cerr << "?UNKNOW?" << endl;
        }
    }
}

int main(int argc, char* argv[])
{
    console();
    /*
    */
	return 0;
}

